<template>
  <!-- 用户获得积分配置页面添加修改页面 -->
  <el-dialog :visible.sync="visible" title="积分配置" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-position="top" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="registerPoints" label="默认签到积分">
        <el-input-number v-model="dataForm.registerPoints" :precision="2" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item prop="registerPointsDesc" label="字段说明">
        <el-input v-model="dataForm.registerPointsDesc"  type="textarea" :autosize="{ minRows: 2, maxRows: 5}" placeholder="请输入字段说明内容" clearable></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',                         //id
        registerPoints:'',              //默认签到积分
        registerPointsDesc: '',         //字段说明
      }
    }
  },
  computed: {
    dataRule() {
      return {
        registerPoints: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        registerPointsDesc: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userRegister/getDefaultSignPoints`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http['post']('/api/userRegister/saveDefaultSignPoints', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
